﻿/**
  * Adds the selected language from the 'available' option box to the 'selected' option box and the 'default language' dropdown
  */
function addLanguages() {
    var lstAvailable = document.getElementById(GetClientId("lstAvailableLangs"));
    var lstSelected = document.getElementById(GetClientId("lstSelectedLangs"));
    var cboDefaultLanguage = document.getElementById(GetClientId("cboDefaultLanguage"));
    if (lstAvailable.options.length > 0 && lstAvailable.selectedIndex >= 0) {
        var currentIdx = lstAvailable.selectedIndex;
        for (var i = lstAvailable.options.length-1; i >= 0; i--) {
            if (lstAvailable.options[i].selected) {
                lstSelected[lstSelected.options.length] = new Option(lstAvailable.options[i].text, lstAvailable.options[i].value, true);
                cboDefaultLanguage.options.add(new Option(lstAvailable.options[i].text, lstAvailable.options[i].value));
                lstAvailable.options[i] = null;
            }
        }
        sortSelect(cboDefaultLanguage);
        sortSelect(lstSelected);
        if (lstAvailable.options.length > currentIdx) {
            lstAvailable.options[currentIdx].selected = true;
        }
    }
    return false;
}

/**
  * Removes the selected language from the 'selected' option box to the 'available' option box and removes it from the 'default language' dropdown
  */
function removeLanguages() {
    var lstAvailable = document.getElementById(GetClientId("lstAvailableLangs"));
    var lstSelected = document.getElementById(GetClientId("lstSelectedLangs"));
    var cboDefaultLanguage = document.getElementById(GetClientId("cboDefaultLanguage"));
    if (lstSelected.options.length > 0 && lstSelected.selectedIndex >= 0) {
        var currentIdx = lstSelected.selectedIndex;
        for (var i = lstSelected.options.length - 1; i >= 0; i--) {
            if (lstSelected.options[i].selected) {
                lstAvailable[lstAvailable.options.length] = new Option(lstSelected.options[i].text, lstSelected.options[i].value, true);
                cboDefaultLanguage.options[i] = null;
                lstSelected.options[i] = null;
            }
        }
        sortSelect(cboDefaultLanguage);
        sortSelect(lstAvailable);
        if (lstSelected.options.length > currentIdx) {
            lstSelected.options[currentIdx].selected = true;
        }
    }
    return false;
}

/*
 * Adds a bundle to the bundles list
 */
function addBundle() {
    var lstBundles = document.getElementById(GetClientId("lstBundles"));
    var txtBundle = document.getElementById(GetClientId("txtNewBundle"));
    var bundleName = txtBundle.value;
    txtBundle.value = "";
    if (bundleName.length > 0) {
        lstBundles.options[lstBundles.options.length] = new Option(bundleName, bundleName);
        sortSelect(lstBundles);
    }
    return false;
}

/*
* Removes a bundle from the bundles list
*/
function removeBundles() {
    var lstBundles = document.getElementById(GetClientId("lstBundles"));
    var currentIdx = lstBundles.selectedIndex;
    for (var i = lstBundles.options.length - 1; i >= 0; i--) {
        if (lstBundles.options[i].selected) {
            lstBundles.options[i] = null;
        }
    }
    if (lstBundles.options.length > currentIdx) {
        lstBundles.options[currentIdx].selected = true;
    }
    return false;
}

/**
  * Sorts the items in a listbox by their value
  * @param list The SELECT element to sort
  */
function sortSelect(list) {
    if (null !== list && list.tagName.toLowerCase() == "select") {
        var len = list.options.length;
        var tmpItems = new Array(len);
        var i;
        var opt;
        for (i = 0; i < len; i++) {
            opt = new Option(list.options[i].text, list.options[i].value);
            opt.selected = list.options[i].selected;
            tmpItems[i] = opt;
        }
        //tmpItems.sort(sortF);
        tmpItems.sort(function(obj, otherObj) {
            return obj.text != otherObj.text ? obj.text < otherObj.text ? -1 : 1 : 0;
        });
        for (i = 0; i < len; i++) {
            opt = new Option(tmpItems[i].text, tmpItems[i].value);
            opt.selected = tmpItems[i].selected;
            list.options[i] = opt;
        }
    }
}